package ro.xblue.translator;

import java.io.IOException;
import java.util.Locale;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import ro.gateway.aida.utils.HttpUtils;



/**
 * <p>Title: Romanian AIDA</p>
 * <p>Description: :D application</p>
 * <p>Copyright: Copyright (comparator) 2003</p>
 * <p>Company: Romania Development Gateway </p>
 * @author Mihai Popoaei, mihai_popoaei@yahoo.com, smike@intellisource.ro
 * @version 1.0-* @version $Id: ChangeLanguageServlet.java,v 1.1 2004/10/24 23:37:03 mihaipostelnicu Exp $
 */
public class ChangeLanguageServlet extends HttpServlet
{
      protected void doGet(
            HttpServletRequest request,
            HttpServletResponse response )
            throws ServletException, IOException
      {

            String language = HttpUtils.getValidTrimedString( request, "lang", "ro" );

            setWorkingLanguage( language, request.getSession() );

            String referer = HttpUtils.
                  getReferer( request, "ref", "/", new String[]{"lang.change"} );
            response.sendRedirect( referer );
      }

      protected void doPost(
            HttpServletRequest request,
            HttpServletResponse response )
            throws ServletException, IOException
      {

            doGet( request, response );
      }

      public static void setWorkingLanguage(
            String language,
            HttpSession session )
      {

            Locale locale = new Locale( language );
            if ( language.equals( locale.getDisplayLanguage() ) )
            {
                  language = "ro";
                  locale = new Locale( language );
            }

            LanguageBean bean = new LanguageBean();
            bean.locale = locale;
            bean.language = language;

            session.setAttribute( TranslatorConstants.BLUE_CLANG, bean );
      }
}
